package UI;

import UI.MainFrame;
import UI.register;
import entity.User;
import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

public class Login extends JFrame implements ActionListener {
    private JLabel username_Label;	//用户名标签。
    private JLabel password_Label;	//密码标签。
    private JTextField username_Text;	//用户名文本域。
    private JPasswordField password_Text;	//密码文本域。
    private JButton login_Button;	//登陆按钮。
    private JButton register_Button;	//注册按钮。
    private JRadioButton admin_Button;  //管理员单选
    private JRadioButton user_Button;   //用户单选
    private JPanel jPanel;
    ButtonGroup group;

    public Login(){

        super("销售管理系统登录界面");
//        this.setSize(400,400);
        this.setResizable(false);

        ImageIcon icon = new ImageIcon("img/logins.jpg");
        //将图片放入label中
        JLabel label = new JLabel(icon);
        //设置label的大小
        label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        this.setSize(icon.getIconWidth(),icon.getIconHeight());
        JPanel panelTop = (JPanel)this.getContentPane();
        panelTop.setOpaque(false);
        this.setLocationRelativeTo(null);


        jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jPanel.setOpaque(false);
        Box boxHOne,boxHTwo,boxHThree,boxHFour; // 行式盒
        Box boxV; // 列式盒

        boxHOne = Box.createHorizontalBox();   //水平
        boxHTwo = Box.createHorizontalBox();
        boxHThree = Box.createHorizontalBox();
        boxHFour = Box.createHorizontalBox();


        boxV = Box.createVerticalBox();  //竖直
        boxV.add(Box.createVerticalStrut(250));


        username_Label = new JLabel("工号：");
        password_Label = new JLabel("密码：");

        username_Text = new JTextField(15);
        password_Text = new JPasswordField(15);
        username_Text.setPreferredSize(new Dimension (15,30));
        password_Text.setPreferredSize(new Dimension (15,30));

        boxHOne.add(username_Label);
        boxHOne.add(Box.createHorizontalStrut(20));
        boxHOne.add(username_Text);
        boxHTwo.add(password_Label);
        boxHTwo.add(Box.createHorizontalStrut(20));
        boxHTwo.add(password_Text);

        boxV.add(boxHOne);
        boxV.add(Box.createVerticalStrut(50));
        boxV.add(boxHTwo);
        boxV.add(Box.createVerticalStrut(50));

        group=new ButtonGroup();    //只能选择其中之一
        admin_Button = new JRadioButton("管理员");
        user_Button = new JRadioButton("员工",true);
        group.add(admin_Button);
        group.add(user_Button);


        login_Button = new JButton("登录");
        register_Button = new JButton("注册");

        boxHThree.add(admin_Button);
        boxHThree.add(Box.createHorizontalStrut(55));
        boxHThree.add(user_Button);
        boxHFour.add(login_Button);
        boxHFour.add(Box.createHorizontalStrut(40));
        boxHFour.add(register_Button);

        boxV.add(boxHThree);
        boxV.add(Box.createVerticalStrut(20));
        boxV.add(boxHFour);
        boxV.add(Box.createVerticalStrut(20));


        jPanel.add(boxV);

        login_Button.addActionListener(this);
        register_Button.addActionListener(this);

        this.add(jPanel);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                close();  //调用close()方法来关闭
            }
        });
    }

    public void close() {    //关闭窗口的操作
        //	使用dispose()方法关闭窗体会释放该窗体的占用的部分资源，不过呢不是全部的
        this.dispose();
        //此语句关闭窗口程序会结束
        System.exit(0);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String username = this.username_Text.getText();
        String password = new String(this.password_Text.getPassword());

        if(e.getSource() == register_Button){
            this.dispose();
            register re = new register();
        }

        if(e.getSource() == login_Button){
            if(username.equals("")){
                JOptionPane.showMessageDialog(this, "员工编号不能为空！", "", JOptionPane.WARNING_MESSAGE);
                return ;
            }
            if(password.equals("")){
                JOptionPane.showMessageDialog(this,"密码不能为空！","",JOptionPane.WARNING_MESSAGE);
                return;
            }
            User user = new User();
            user.setWorkNo(username);
            user.setPassword(password);
            //判断是否为管理员
            if(admin_Button.isSelected()){
                user.setFlag(true);
            }
            try {
                JdbcDeal jdbcDeal = new JdbcDeal();
                if(jdbcDeal.IsLogin(user)){
                    JOptionPane.showMessageDialog(this,"登录成功!","",JOptionPane.PLAIN_MESSAGE);
                    jdbcDeal.close();
                    this.dispose();
                    MainFrame mainFrame = new MainFrame(user);
                }else {
                    JOptionPane.showMessageDialog(this, "密码错误!", "", JOptionPane.WARNING_MESSAGE);
                    jdbcDeal.close();
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new Login();
    }
}
